#include "loginmanage.h"
#include "ui_loginmanage.h"
#include<QMessageBox>
#include"ClientSocket.h"
#include"vector_serialization.h"
#include"SocketException.h"
#include<iostream>
#include<openssl/md5.h>
#include "manageinfo.h"
loginmanage::loginmanage(QWidget *parent) :
        QWidget(parent),
        ui(new Ui::loginmanage)
{
        ui->setupUi(this);
        connect(ui->pushButton_submit,SIGNAL(clicked()),
                this, SLOT(handleSubmit()));
}

loginmanage::~loginmanage()
{
        delete ui;
}

void loginmanage::handleSubmit() {
        //QString a = ui->lineEdit_uname->text();
        //QMessageBox::about(0,"hello",a);
        unsigned char md[16];
        std::string sendmsg, reply;
        std::string uname, upasswd, urole;
        //urole "m" represent maneger
        urole = "m";
        uname = ui->lineEdit_uname->text().toStdString();
        upasswd = ui->lineEdit_upasswd->text().toStdString();
        map<std::string, std::string> smap;
        smap["code"] = "002";
        smap["uname"] = uname;
        //MD5((const unsigned char *)upasswd.c_str(), strlen(upasswd.c_str()), md);
        //std::cout << "md5:" << md << std::endl;
        //upasswd = std::string((char *)md,16);
        //std::cout << "upasswd:" << upasswd << std::endl;
        smap["upasswd"] = upasswd;
        smap["urole"] = urole;
        sendmsg = save(smap);
        ClientSocket client_socket("localhost",30000);
        client_socket << sendmsg;
        client_socket >> reply;
        vector<string> svec;
        load(reply,svec);

        if (svec.size() == 1)
                reply = *(svec.begin());
        for (vector<string>::iterator it = svec.begin(); it != svec.end(); ++it) {
                std::cout << *it;
        }
        if (reply.compare("empty")) {

                manageinfo *mg = new manageinfo(0, this, uname);
                mg->show();
        }
        else {
                QMessageBox::about(0, "reply:", "authentic failed!");
        }
}
